
// Figure 1
clear
use working/sequential_records_with_geography
keep if new_account
gen yearm = mofd(date)
format yearm %tmCY-m
collapse (first) city bank (sum) new_account , by(block_id  yearm)
tsset block_id yearm
tsfill
foreach var of varlist new_account  {
	replace `var' = 0 if mi(`var')
}
drop if block_id ==3
gen year = year(dofm(yearm))
la var yearm "Year-Month"
format yearm %tmm-CY
keep if inrange(yearm, ym(1866,1),ym(1874,5))
segraph new_account, by(yearm) line nose ///
	ytitle(Average New Accounts Per Month) ///
	scheme(cleanplots) legend(off) xtitle("")

graph export output/avg_accounts_over_time.pdf, replace


// Figure 2
use working/sequential_records_with_geography, clear
keep if new_account
capture drop yearm
gen yearm = mofd(date - 1)
clonevar yearm2 = yearm
replace yearm2 = yearm2 - 1 if mod(yearm,2)==0
format yearm %tmm-CY
la var yearm "Year-Month"
la var white "Fraction of White New Depositors"
segraph white if inrange(yearm, ym(1866,1),ym(1874,5)) & !inlist(city, "New York"), by(yearm) ///
	refline( ym(1868,10) ym(1871,6)  ym(1873,6)) ///
	scale(1) graphopt(scheme(modern) legend(pos(6))) setype(seb)  xtitle("")

graph export output/white_depositors_by_month.pdf, replace


// Figure 3

clear
use working/jay_cooke_window
drop if city == "Lexington" | city == "Philadelphia"

la var date "Date" 
format date %tdm_d,_CY

by bank: gen cum_new_accts = sum(new_accounts)
by bank: egen accts_at_event = total(cond(date == mdy(9,18,1873), cum_new_accts, .))

gen cum_accts_scaled = cum_new_accts/accts_at_event

la var cum_new_accts "Cumulative New Accounts"
la var cum_accts_scaled "Cumulative New Accounts, Scaled"


xtline cum_new_accts, ///
plot1(lpattern(dash)) plot2(lpattern(---....)) ///
plot3(lpattern(dash_dot)) plot4(lpattern(shortdash)) plot5(lpattern(longdash)) ///
xline(-31515) scheme(modern) tlabel(-31594 -31532 -31471 -31410) ///
overlay title("Cumulative New Accounts") subtitle("July - December 1873")  legend(off)

graph export output/cum_accts.pdf, replace

xtline cum_accts_scaled, ///
plot1(lpattern(dash)) plot2(lpattern(---....)) ///
plot3(lpattern(dash_dot)) plot4(lpattern(shortdash)) plot5(lpattern(longdash)) ///
xline(-31515)  scheme(modern)  tlabel(-31594 -31532 -31471 -31410) ///
overlay title("Cumulative New Accounts, Scaled by Accounts at Event") ///
subtitle("July - December 1873") legend(off)


graph export output/cum_accts_scaled.pdf, replace



// Figure 4
clear
use working/sequential_records_with_geography
keep if new_account

gen nonwhite = white==0
replace local = . if white==1
replace organization = . if white==1
drop nonlocal
gen nonlocal = local==0
gen individual = organization==0
collapse (sum) new_account white nonwhite local nonlocal organization individual, by(date)

tsset date
tsfill, full
foreach var of varlist new_account white local organization {
	replace `var' = 0 if mi(`var')
}

foreach var of varlist new_account white nonwhite local nonlocal organization individual {
local roll_avg `var'
forval x = 1/6 {
	local roll_avg `roll_avg' + cond(!mi(l`x'.`var'),l`x'.`var',0)
}
	gen `var'_avg = `roll_avg'/7
}


la var white_avg "White"
la var nonwhite_avg "Nonwhite"

la var local_avg "Local"
la var nonlocal_avg "Nonlocal"

la var organization_avg "Organization"
la var individual_avg "Individual"

la var date "Date"
format date %tdm_d,_CY

twoway (tsline white_avg  if date>=mdy(1,7,1873)) (tsline nonwhite_avg  if date>= mdy(1,7,1873), yaxis(2)), scheme(cleanplots) legend(position(6) rows(1)) 
graph export output/new_depositors1.pdf, replace

twoway (tsline local_avg  if date>=mdy(1,7,1873) & white!=1) (tsline nonlocal_avg if date>=mdy(1,7,1873) & white!=1, yaxis(2)), scheme(cleanplots) legend(position(6) rows(1)) 
graph export output/new_depositors2.pdf, replace

twoway (tsline organization_avg  if date>=mdy(1,7,1873) & white!=1) (tsline individual_avg if date>=mdy(1,7,1873) & white!=1, yaxis(2)), scheme(cleanplots) legend(position(6) rows(1)) 
graph export output/new_depositors3.pdf, replace

// Figure 5

use "working/categorized_valid_sequential_records.dta", clear


collapse (sum) new_account, by(date)
drop if date < mdy(1,1,1873)
sort date
gen cum_accts = new_account if date == mdy(1, 1, 1873)
replace cum_accts = new_account + cum_accts[_n-1] if date != mdy(1, 1, 1873)

label variable date "Date"
label variable cum_accts "Cumulative New Accounts"

format date %tdm-CY

* This is Sept 22, 1873: xline(-31511)
twoway line cum_accts date if inrange(date, mdy(1,1,1873), mdy(12,30,1873)) , lcolor(blue) lpattern(solid) || ///
 lfit cum_accts date if inrange(date, mdy(1,1,1873), mdy(9,21,1873)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(9,22,1873), mdy(12,30,1873)), lcolor(black) lpattern(dash)  ///
 , xline(-31511) legend(off) ytitle("Cumulative New Accounts") xlabel(,labs(small)) scheme(modern)
 
 graph export output/line_of_fit1.pdf, replace


* This is Dec 25, 1873: xline(-31416)
twoway line cum_accts date if inrange(date, mdy(9,22,1873), mdy(4,10,1874)), lcolor(blue) lpattern(solid)  || ///
 lfit cum_accts date if inrange(date, mdy(9,22,1873), mdy(12,25,1873)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(12,26,1873), mdy(4,10,1874)), lcolor(black) lpattern(dash) ///
 , xline(-31416) legend(off) ytitle("Cumulative New Accounts") xlabel(,labs(small))  scheme(modern)
graph export output/line_of_fit2.pdf, replace

* This is April 10, 1874: xline(-31311)
twoway line cum_accts date if inrange(date, mdy(1,1,1874), mdy(7,4,1874)), lcolor(blue) lpattern(solid)  || ///
 lfit cum_accts date if inrange(date, mdy(1,1,1874), mdy(4,9,1874)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(4,10,1874), mdy(7,4,1874)), lcolor(black) lpattern(dash) ///
 , xline(-31311) legend(off) ytitle("Cumulative New Accounts") xlabel(,labs(small))  scheme(modern)
 graph export output/line_of_fit3.pdf, replace
 
 
 
** Do all 3 together 
twoway line cum_accts date if inrange(date, mdy(1,1,1873), mdy(7,4,1874)), lcolor(blue) lpattern(solid) || ///
 lfit cum_accts date if inrange(date, mdy(1,1,1873), mdy(9,21,1873)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(9,22,1873), mdy(12,25,1873)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(12,26,1873), mdy(4,9,1874)), lcolor(black) lpattern(dash) || ///
 lfit cum_accts date if inrange(date, mdy(4,10,1874), mdy(7,4,1874)), lcolor(black) lpattern(dash) ///
 , xline(-31511 -31416 -31311) legend(off) ytitle("Cumulative New Accounts") xlabel(,labs(small))  scheme(modern)
graph export output/line_of_fit4.pdf, replace


// Figure 6

use working/fs_accounts_per_day, clear
drop if city == "Philadelphia"
collapse (sum) new_accounts, by(date)
keep if inrange(date, mdy(2,1,1874), mdy(6,30,1874))
drop if dow(date)==0
bcal create bank_date, from(date) g(bank_date) replace
format bank_date %tbbank_date:m-CY
tsset bank_date
gen cum_accounts = sum(new_accounts)

la var bank_date "Date"
la var new_accounts "Daily New Accounts"
la var cum_accounts "Cumulative New Accounts"
tsline new_accounts, tline(58 72)  ttitle("") tlabel(0 24 50 76 102) scheme(modern) 
graph copy g1, replace
tsline cum_accounts, tline(58 72) tlabel(0 24 50 76 102) scheme(modern)
graph copy g2, replace
graph combine g1 g2,  col(1) scheme(modern)
graph export output/struc_break3_combined.pdf, replace


// Figure 7

use working/passbook_regimes, clear
sort id  date  transaction_type_order  amt
keep if date<=mdy(6,30,1874)
gen yearm = mofd(date)
format yearm %tmCY-n
sort id  date  transaction_type_order  amt
drop if running_total<=0

gen account_group = .
by id: replace account_group = 1 if inrange(date[1], mdy(1,1,1872), mdy(8,30,1873))
by id: replace account_group = 2 if inrange(date[1], mdy(9,18,1873), mdy(12,31,1874))
by id: replace account_group = 3 if inrange(date[1], mdy(1,1,1874), mdy(4,9,1874))
 by id: replace account_group = 4 if inrange(date[1], mdy(4,10,1874), mdy(6,30,1874))

gen account_group2 = .
by id: replace account_group2 = 1 if inrange(yearm[1],  ym(1872,1), ym(1873,8))
by id: replace account_group2 = 2 if inrange(yearm[1],  ym(1873,9), ym(1873,12))
by id: replace account_group2 = 3 if inrange(yearm[1], ym(1874,1), ym(1874,12))


collapse (first) month_open =running_total (last) month_close =running_total, by(id yearm city name account_group*)
tsset id yearm
tsfill, full
foreach var of varlist city name month_open month_close account_group* {
	by id: replace `var' = `var'[_n-1] if mi(`var')
}
drop if mi(month_close)
qui duplicates report id
local nid = r(unique_value)
count if yearm == ym(1874,6)
local count_final = r(N)
assert `nid' == `count_final'

by id: egen open_bal = mean(cond(inrange(_n,1,3),month_close,.))
by id: egen max_bal = max(month_close)

by id: gen _temp1 = open_bal if _n==1
by id: gen _temp2 = max_bal if _n==1
xtile acct_size_open = _temp1, nq(3)
xtile acct_size_max = _temp2, nq(3)
by id: replace acct_size_open= acct_size_open[1]
by id: replace acct_size_max= acct_size_max[1]
by id: gen account_open_date = yearm[1]

by id: egen mean_balance = mean(month_close)
by id: gen pct_of_mean = month_close/mean_balance


gen yearm_bin = yearm +1056 if yearm>=-1056
label def yearm_bin , replace
forval x = -1056/-1027 {
	local i = `x' + 1056
	local time_lab : di %tmCY-n `x'
	label def yearm_bin `i' "`time_lab'", add
}
la val yearm_bin yearm_bin
replace yearm_bin = . if yearm_bin<0
la var pct_of_mean "End of Month Balance as % of Mean"
la var yearm "Year-Month"
format yearm %tmm-CY

segraph pct_of_mean if yearm>ym(1873,1) & account_group ==1 , by(yearm) scale(1)  refline(ym(1873,9) ym(1873,12) ym(1874,4))  xtitle("") graphopt(scheme(modern) legend(off))
graph copy g1, replace
graph export output/depositor_balance_graph_regime1.pdf, replace

segraph pct_of_mean if yearm>=ym(1873,10) & account_group ==2 , by(yearm) scale(1)  refline(ym(1873,12) ym(1874,4))  xtitle("") graphopt(scheme(modern) legend(off))
graph copy g2, replace
graph export output/depositor_balance_graph_regime2.pdf, replace

segraph pct_of_mean if yearm>ym(1873,1) & account_group ==3 , by(yearm) scale(1)  refline( ym(1874,4))  xtitle("") graphopt(scheme(modern) legend(off))
graph copy g3, replace
graph export output/depositor_balance_graph_regime3.pdf, replace


// Figure 8
clear
use working/weekly_closure_test_data
* Drop cities which do not have contiguous records up to the failure.
drop if inlist(city, "Tallahassee", "Charleston")
* Drop discontiguous 1872 Beaufort records
drop if city=="Beaufort" & date < mdy(1,14 ,1873)
keep if new_account
keep if yearm >= ym(1872,1)
format yearm %tmm-CY


capture drop regime
gen regime = .
local n = 0
local b = mdy(1,1,1872) 
foreach date_break in	mdy(9,21,1873) ////
											mdy(12,26,1873) mdy(4,10,1874) mdy(8,1,1874) {
	di %tdm_D,_CY `date_break'
	local e = `date_break' - 1
	replace regime = `n' if inrange(date,`b',`e')
	local b = `date_break'
	local n = `n' + 1
}


qui sum regime
local maxval = r(max)

capture label drop regime
forval x = 0/`maxval' {
	qui sum date if regime==`x'
	di "`x' " %td `r(min)' " - " %td `r(max)'
	* local dumlab : di "`x' " %td `r(min)' " - " %td `r(max)'
	local dumlab : di %tdm-D/Y `r(min)' " - " %tdm-D/Y `r(max)'
	la define regime `x' "`dumlab'", add
}

la val regime regime
la var regime "Dates"
la var closed_account "% closed"
la var white "Race"
la var organization "Account"
la var local "Born/Raised in State"
la define white 1 "White" 0 "Nonwhite", replace
la define local 1 "Local" 0 "Nonlocal", replace
la define organization 1 "Organization" 0 "Personal", replace
la val white white
la val local local
la val organization organization




segraph closed_account , by(yearm) sub(regime) scale(1) scheme(cleanplots) legend(position(6) rows(2)) xtitle ("")
graph export output/closure_regime.pdf, replace

local refline3 = -1029
local refline2 = -1033
local refline1 = -1036

segraph closed_account if organization!=1, by(yearm) sub(white) scale(1)  scheme(cleanplots) legend(position(6) rows(1)) xtitle("") refline(`refline1' `refline2' `refline3')
graph export output/closure_white.pdf, replace

segraph closed_account if white!=1 & organization!=1, by(yearm) sub(local) scale(1)  scheme(cleanplots) legend(position(6) rows(1)) xtitle("") refline(`refline1' `refline2' `refline3')
graph export output/closure_local.pdf, replace

segraph closed_account , by(yearm) sub(organization) scale(1) scheme(cleanplots) legend(position(6) rows(1)) xtitle("") refline(`refline1' `refline2' `refline3')
graph export output/closure_organization.pdf, replace







